package com.eds.redis;

import com.eds.dao.RedisDao;
import com.eds.log.LogUtil;
import com.eds.pojo.RecommendationDTO;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class RedisRecommendationCacheService {
    private static final Logger logger = LogUtil.getLogger(RedisRecommendationCacheService.class);

    @Resource
    private RedisDao<Object> redisDao;

    // 用户推荐缓存键前缀
    private static final String USER_RECOMMENDATIONS_KEY_PREFIX = "recommendation:user:";

    // 热门推荐缓存键
    private static final String HOT_RECOMMENDATIONS_KEY = "recommendation:hot";

    //开心一刻推荐
    private static final String HAPPY_RECOMMENDATIONS_KEY = "recommendation:happy";

    // 缓存用户推荐
    public void cacheUserRecommendations(Long userId, List<RecommendationDTO> recommendations) {
        try {
            String key = USER_RECOMMENDATIONS_KEY_PREFIX + userId;
            redisDao.set(key, recommendations, 7, TimeUnit.DAYS);
            logger.info("成功缓存用户{}的推荐数据，共{}个推荐", userId, recommendations.size());
        } catch (Exception e) {
            logger.error("缓存用户{}的推荐数据失败", userId, e);
        }
    }

    // 获取用户推荐
    public List<RecommendationDTO> getUserRecommendations(Long userId) {
        try {
            String key = USER_RECOMMENDATIONS_KEY_PREFIX + userId;
            Object value = redisDao.get(key);
            if (value != null) {
                if (value instanceof List) {
                    return ((List<?>) value).stream()
                            .filter(item -> item instanceof RecommendationDTO)
                            .map(item -> (RecommendationDTO) item)
                            .collect(Collectors.toList());
                }
            }
        } catch (Exception e) {
            logger.error("获取用户{}的推荐数据失败", userId, e);
        }
        return Collections.emptyList();
    }

    // 缓存热门推荐
    public void cacheHotRecommendations(List<RecommendationDTO> recommendations) {
        try {
            redisDao.set(HOT_RECOMMENDATIONS_KEY, recommendations, 6, TimeUnit.HOURS);
            logger.info("成功缓存热门推荐数据，共{}个推荐", recommendations.size());
        } catch (Exception e) {
            logger.error("缓存热门推荐数据失败", e);
        }
    }

    // 获取热门推荐
    public List<RecommendationDTO> getHotRecommendations() {
        try {
            Object value = redisDao.get(HOT_RECOMMENDATIONS_KEY);
            if (value != null) {
                if (value instanceof List) {
                    return ((List<?>) value).stream()
                            .filter(item -> item instanceof RecommendationDTO)
                            .map(item -> (RecommendationDTO) item)
                            .collect(Collectors.toList());
                }
            }
        } catch (Exception e) {
            logger.error("获取热门推荐数据失败", e);
        }
        return Collections.emptyList();
    }

    //缓存开心一刻推荐
    public void cacheHappyRecommendations(List recommendations) {
        try {
            redisDao.set(HAPPY_RECOMMENDATIONS_KEY, recommendations, 6, TimeUnit.HOURS);
            logger.info("成功缓存开心一刻推荐数据，共{}个推荐", recommendations.size());
        } catch (Exception e) {
            logger.error("缓存开心一刻推荐数据失败", e);
        }
    }

    //获取开心一刻推荐
    public Object getHappyRecommendations() {
        try {
            Object value = redisDao.get(HAPPY_RECOMMENDATIONS_KEY);
            return value;
        } catch (Exception e) {
            logger.error("获取开心一刻推荐数据失败", e);
        }
        return null;
    }
}